<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Model\Admin;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;
use Validator;


class AdminController extends CommonController
{
    //管理员列表
    public function index()
    {
        $admins = Admin::paginate(5);
        return view('admin.admin.index', compact('admins'));
    }

    //新增管理员
    public function adminadd(Request $request)
    {

        if ($request->isMethod('post')) {
            $messages = [
                'username.required' => '用户名不能为空',
                'password.required' => '新密码不能为空',
                'password_confirmation.required' => '重复密码不能为空',
                'password.confirmed' => '新旧密码不一致'
            ];

            $validator = Validator::make($request->all(), [
                'username' => [
                    'required',
                    function ($attribute, $value, $fail) {

                        if (Admin::where('username', $value)->first()) {
                            return $fail('用户名已存在');
                        }
                    }
                ],
                'password' => [
                    'required',
                    'confirmed'
                ],
                'password_confirmation' => [
                    'required'
                ]
            ], $messages);

            if ($validator->fails()) {
                return redirect('/admin/adminadd')->withErrors($validator)->withInput();
            }
            //新增管理员
            $admin = new Admin;
            $admin->username = $request->username;
            $admin->password = bcrypt($request->password);
            $admin->save();
            return redirect('/admin/admin')->with('status', '新增成功');
        }
        return view('admin.admin.adminadd');
    }

    //修改密码
    public function changepassword(Request $request)
    {
        //验证密码
        if ($request->isMethod('post')) {
            $messages = [
                'oldpwd.required' => '旧密码不能为空',
                'newpwd.required' => '新密码不能为空',
                'newpwd_confirmation.required' => '重复密码不能为空',
                'newpwd.confirmed' => '新旧密码不一致'
            ];

            $validator = Validator::make($request->all(), [
                'oldpwd' => [
                    'required',
                    function ($attribute, $value, $fail) {
                        if (!Hash::check($value, Auth::user()->getAuthPassword())) {
                            return $fail('旧密码不对哦');
                        }
                    },
                ],
                'newpwd' => [
                    'required',
                    'confirmed'
                ],
                'newpwd_confirmation' => [
                    'required'
                ]
            ], $messages);

            if ($validator->fails()) {
                return redirect('/admin/changepassword')->withErrors($validator)->withInput();
            }
            $adminM = Auth::user();
            $adminM->password = bcrypt($request->newpwd);
            if ($adminM->save()) {
                Auth::logout();
                return redirect('admin/login')->with('status', '修改成功，请重新登录');
            }
        }

        return view('admin.admin.changepassword');
    }

    //删除管理员
    public function admindel($id)
    {
        if ($id == 1) {
            return redirect('admin/admin')->with('status', '超级管理员不能删除');
        }
        $admin = Admin::find($id);
        $admin->delete();
        return redirect('admin/admin')->with('status', '删除成功');
    }

}
